import cv2
import os
import matplotlib.pyplot as plt

filename = 'teachers_code/lena.bmp'

def plotGrayHist():
  '绘制灰度直方图'
  image = cv2.imread(filename,cv2.IMREAD_COLOR)
  image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
  r,g,b = cv2.split(image)
  ax1 = plt.subplot(221)
  ax1.set_title(os.path.basename(filename))
  plt.imshow(image)
  # 隐藏坐标轴刻度
  plt.xticks([])
  plt.yticks([])
  # 绘制RGB灰度直方图
  ax2 = plt.subplot(222)
  ax2.set_title('RED')
  plt.hist(r.ravel(),256,[0,256])
  ax3 = plt.subplot(223)
  ax3.set_title('GREEN')
  plt.hist(g.ravel(),256,[0,256])
  ax4 = plt.subplot(224)
  ax4.set_title('BLUE')
  plt.hist(b.ravel(), 256, [0, 256])
  plt.show()


plotGrayHist()

